﻿using System;
using System.Linq;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Mutzl.Samples.VideoApp.YouTube.Model;
using YouTubeHelper = MyToolkit.Multimedia;

namespace Mutzl.Samples.VideoApp.YouTube.ViewModel
{
    public class DetailViewModel : ViewModelBase
    {
        // Dieser Konstruktor ist nur für Designtime vorgesehen
        [PreferredConstructor]
        public DetailViewModel()
        {
            if (IsInDesignMode)
            {
                // ... und holt sich einfach das erste Video
                Video = Video.CreateDesignVideo();
            }

        }


        public DetailViewModel(Video video)
        {
            Video = video;

            UpdateVideoUri();
            Video.PropertyChanged += (sender, args) =>
                                         {
                                             switch (args.PropertyName.ToLower())
                                             {
                                                 case "duration": 
                                                     RaisePropertyChanged(() => Duration);
                                                     break;
                                                 case "date":
                                                     RaisePropertyChanged(() => Date);
                                                     break;
                                                 case "id":
                                                     UpdateVideoUri();
                                                     break;
                                             }
                                         };
        }

        private Video _video;
        public Video Video
        {
            get { return _video; }
            set
            {
                if (_video == value) return;
                _video = value;
                RaisePropertyChanged(() => Video);
            }
        }

        public Uri VideoUri { get; private set; }
        private async void UpdateVideoUri()
        {
            var url = await YouTubeHelper.YouTube.GetVideoUriAsync(Video.Id, YouTubeHelper.YouTubeQuality.Quality1080P);
            VideoUri = url.Uri;
            RaisePropertyChanged(() => VideoUri);
        }

        public string Duration { get { return (Video == null) ? "--" : string.Format("Dauer: {0}", Video.Duration); } }
        public string Date { get { return (Video == null) ? " --" : string.Format("Datum: {0:dd.MM.yyyy}", Video.Date); } }

        private bool _isFullScreen;
        public bool IsFullScreen
        {
            get { return _isFullScreen; }
            set
            {
                if (_isFullScreen == value) return;
                _isFullScreen = value;
                RaisePropertyChanged(() => IsFullScreen);
            }
        }

    }
}